package com.hgk.common.common.ui.view;

import android.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

import com.common.dongqiuhui.apps.comm.tools.BallUtil;

public class CircularImageView extends ImageView {
	private int borderWidth = 2;
	private int viewWidth;
	private int viewHeight;
	private Bitmap bitmap;
	private Drawable drawable;
	private Paint paint;
	private Paint paintBorder;
	private BitmapShader shader;
	private int action;
    private Context mContext;
	public CircularImageView(Context context) {
		super(context);
		init(context);
	}

	public CircularImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public CircularImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	private int measureWidth(int measureSpec) {
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);
		if (specMode == MeasureSpec.EXACTLY)
			return specSize;
		return viewWidth;
	}

	private int measureHeight(int measureSpecHeight, int measureSpecWidth) {
		int specMode = MeasureSpec.getMode(measureSpecHeight);
		int specSize = MeasureSpec.getSize(measureSpecHeight);
		if (specMode == MeasureSpec.EXACTLY)
			return specSize;
		return viewHeight;
	}

	private void init(Context mContext) {
		this.setScaleType(ScaleType.CENTER_CROP);
		paint = new Paint();
		paint.setAntiAlias(true);
		paintBorder = new Paint();
		paintBorder.setAntiAlias(true);
		borderWidth= BallUtil.dip2px(mContext, borderWidth);
		//setBorderColor(0x80FFFFFF);
		setBorderColor(mContext.getResources().getColor(R.color.white));
	}

	public void setBorderColor() {
		setBorderColor(Color.TRANSPARENT);
	}

	public void setBorderColor(int rscId) {
		if (this.paintBorder != null)
			this.paintBorder.setColor(rscId);
		invalidate();
	}

	/**
	 * Never allocate new object instance frequently which may cause gc hardly
	 */
	private void updateBitmap() {
		if (drawable == null || drawable != getDrawable()) {
			drawable = getDrawable();
			if (drawable instanceof BitmapDrawable) {
				bitmap = ((BitmapDrawable) drawable).getBitmap();
			} else if (drawable instanceof TransitionDrawable) {
				TransitionDrawable transitionDrawable = (TransitionDrawable) drawable;
				BitmapDrawable bitmapDrawable = (BitmapDrawable) transitionDrawable
						.getDrawable(transitionDrawable.getNumberOfLayers() - 1);
				if (bitmapDrawable != null) {
					bitmap = bitmapDrawable.getBitmap();
				}
			}
			if (bitmap != null) {
				if(bitmap.getWidth()>bitmap.getHeight())
				{
					bitmap=Bitmap.createBitmap(bitmap, (bitmap.getWidth()-bitmap.getHeight())/2, 0, bitmap.getHeight(), bitmap.getHeight());
				}else if(bitmap.getWidth()<bitmap.getHeight())
				{
					bitmap=Bitmap.createBitmap(bitmap, 0, (bitmap.getHeight()-bitmap.getWidth())/2, bitmap.getWidth(), bitmap.getWidth());
				}
				shader = new BitmapShader(Bitmap.createScaledBitmap(bitmap,
						viewWidth, viewHeight, false), Shader.TileMode.CLAMP,
						Shader.TileMode.CLAMP);
				paint.setShader(shader);
			}
		}
	}

	@Override
	public void onDraw(Canvas canvas) {
		try {
			updateBitmap();
			if (bitmap != null) {
				int circleCenter = viewWidth / 2;
				canvas.drawCircle(circleCenter, circleCenter, circleCenter, paintBorder);
				canvas.drawCircle(circleCenter, circleCenter, circleCenter - borderWidth, paint);
			}
			switch (this.action) {
				case MotionEvent.ACTION_DOWN:
					setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
				default:
					break;
			}
			clearColorFilter();
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}

	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		viewWidth = measureWidth(widthMeasureSpec);
		viewHeight = measureHeight(heightMeasureSpec, widthMeasureSpec);
		setMeasuredDimension(viewWidth, viewHeight);
	}

	public boolean onTouchEvent(MotionEvent ev) {
		action = ev.getAction();
		return super.onTouchEvent(ev);
	}

	public void setBorderWidth(int borderWidth) {
		this.borderWidth = borderWidth;
		invalidate();
	}
}